package com.anbinh.doctor.fragment.lichkham;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.adapter.LichkhamAdapter;
import com.anbinh.doctor.model.DateTime;
import com.anbinh.doctor.model.ItemTime;
import com.anbinh.doctor.model.ResponseSchedules;
import com.anbinh.doctor.model.ResponseSchedules.Data.Item;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.IRcallBak;
import com.anbinh.doctor.utils.Utils;
import com.krish.horizontalscrollview.CenterLockHorizontalScrollview;
import com.krish.horizontalscrollview.CustomListAdapter;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;

public class FragmentLichkham extends Fragment implements IRcallBak {
	public static FragmentLichkham fragment = null;
	private View mView;
	private ListView mListview;
	private CircleProgressBar loadding;

	private List<ItemTime> mListTime;
	private LichkhamAdapter adapter;
	private CustomListAdapter customListAdapter;
	private CenterLockHorizontalScrollview centerLockHorizontalScrollview;
	private String mCurrentDay;
	private List<Item> mListData;
	private Item mCurrentSchedules = null;
	private UserLogin user;

	public static FragmentLichkham newInstance() {
		if (fragment == null) {
			fragment = new FragmentLichkham();
		}
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		user = Utils.getAccountModel(getActivity(), Utils.KEY_USER);
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_lichkham, null);
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		loadding = (CircleProgressBar) mView.findViewById(R.id.loadding);
		mListview = (ListView) mView.findViewById(R.id.listView1);
		onloadData();
		onDisplayTime(-1, Integer.parseInt(Utils.getCurrentDay()) - 1);
		mListview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				String selectDate = FragmentLichkham.newInstance()
						.getmCurrentDay();
				if (!Utils.isCheckPassDay(selectDate)) {
					Utils.showMessage(getActivity(),
							getResources().getString(R.string.chon_lai_ngay));
					return;
				}
				mCurrentSchedules = mListData.get(position);
				Bundle bundle = new Bundle();
				bundle.putInt(Utils.KEY, Utils.EDIT_LICH_KHAM);
				bundle.putInt(Utils.REQUEST_CODE, Utils.EDIT_LICH_KHAM);

				bundle.putBoolean(Utils.CUSTOMBAR, true);
				bundle.putString(Utils.TITLE,
						getResources().getString(R.string.cap_nhat_lich_kham));
				Utils.getNewIntent(getActivity(), bundle);

			}
		});
	}

	public Item getCurrentSchedules() {
		return mCurrentSchedules;
	}

	public void onDisplayTime(int month, final int currentDay) {
		int year = Integer.parseInt(Utils.getCurrentYear());
		int mMonth = month;
		if (month == -1) {
			mMonth = Integer.parseInt(Utils.getCurrentMonth());
		}
		Log.d("fullDate", mMonth + "");

		int lastDay = Integer.parseInt(Utils.getLastDay(mMonth, year));

		final ArrayList<DateTime> mListDate = new ArrayList<DateTime>();
		for (int i = 0; i < lastDay; i++) {
			String day = (i + 1) + "";
			DateTime item = new DateTime();
			item.setDate(day);
			item.setFullDate(Utils.getDateFormat(year + "-" + mMonth + "-"
					+ day));

			String dayName = Utils.getDayName(
					Integer.parseInt(Utils.getCurrentYear()), mMonth, (i + 1));
			item.setDateName(Utils.getMapDate().get(dayName));
			mListDate.add(item);
		}

		centerLockHorizontalScrollview = (CenterLockHorizontalScrollview) mView
				.findViewById(R.id.scrollView);
		customListAdapter = new CustomListAdapter(getActivity(),
				R.layout.item_date_time, mListDate, new IRcallBak() {

					@Override
					public void onDisplay(int type, Bundle data) {
						mCurrentDay = mListDate.get(type).getFullDate();
						centerLockHorizontalScrollview.setCenter(type);
						onloadData();
					}
				});

		centerLockHorizontalScrollview.setAdapter(getActivity(),
				customListAdapter);
		new CountDownTimer(500, 100) {

			@Override
			public void onTick(long millisUntilFinished) {
			}

			@Override
			public void onFinish() {
				mCurrentDay = mListDate.get(currentDay).getFullDate();
				centerLockHorizontalScrollview.setCenter(currentDay);
				onloadData();
			}
		}.start();

	}

	public String getmCurrentDay() {
		return mCurrentDay;
	}

	public void onShowDelete(boolean isShow) {
		if(mListData==null||mListData.size()==0)
			return;
		
		for (int i = 0; i < mListData.size(); i++) {
			mListData.get(i).setEdit(isShow);
		}
		adapter.notifyDataSetChanged();
	}

	public void onloadData() {
		String token = user.getData().getToken();
		loadding.setVisibility(View.VISIBLE);
		ServerController.getInstance(getActivity()).reuquestListSchedule(token,
				FragmentLichkham.newInstance().getmCurrentDay(), 10,-1,
				new OnRequest<ResponseSchedules>() {

					@Override
					public void onSuccess(ResponseSchedules data) {
						loadding.setVisibility(View.INVISIBLE);
						if (getActivity() == null)
							return;
						if (data.getResponsecode() < 0)
							return;

						mListData = data.getData().getlistSchedules();
						if (mListData == null || mListData.size() == 0) {
							mListview.setVisibility(View.INVISIBLE);
							return;
						}

						mListview.setVisibility(View.VISIBLE);
						adapter = new LichkhamAdapter(getActivity(),
								R.layout.item_lich_kham, mListData,-1,
								new IRcallBak() {

									@Override
									public void onDisplay(int type, Bundle data) {
										if (type == Utils.GONE_PROCESS) {
											loadding.setVisibility(View.INVISIBLE);
											Utils.disableEnableControls(true,
													mView);
										} else {
											loadding.setVisibility(View.VISIBLE);
											Utils.disableEnableControls(false,
													mView);
										}

									}
								});

						mListview.setAdapter(adapter);

					}

					@Override
					public void onError(int errorCode, String message) {
						loadding.setVisibility(View.INVISIBLE);
					}
				});

	}

	@Override
	public void onDisplay(int type, Bundle data) {

	}

}
